<?php

namespace App\Api\Repositories\Log;

use App\Models\Log\LogUserCollect as Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class LogUserCollectRepositories
{
    protected $eloquentClass = Model::class;

    /**
     * 获取收藏记录是否存在
     *
     *
     * @param string $id
     * @param int $uid
     * @return void
     */
    public function get_is_collect($id, $uid)
    {
        $is_collect = Redis::get("sic:{$uid}:{$id}");
        if ($is_collect === NULL) {
            $res = $this->eloquentClass::where('id', $id)->where('uid', $uid)->first() ? true : false;
            Redis::set("sic:{$uid}:{$id}", $res);
            $is_collect = Redis::get("sic:{$uid}:{$id}");
        }
        return boolval($is_collect);
    }

    /**
     * 添加收藏记录
     *
     * @param string $id 搜索id
     * @param int $uid
     * @return void
     */
    public function set_is_collect($id, $uid)
    {
        $is_collect = $this->get_is_collect($id, $uid);
        if ($is_collect) {
            $this->eloquentClass::where('uid', $uid)->where('result_id', $id)->delete();
        } else {
            $this->eloquentClass::create([
                'uid' => $uid,
                'result_id' => $id
            ]);
        }
        $now_is_collect = $is_collect ? '0' : '1';
        Redis::set("sic:{$uid}:{$id}", $now_is_collect);
        $this->add_user_be_collect_count(substr($id, 1, 100), $now_is_collect);
        $this->add_user_collect_count($uid, $now_is_collect);
        return $now_is_collect;
    }

    /**
     * 获取会员被收藏的次数
     *
     * @param int $uid
     * @return void
     */
    public function get_user_be_collect_count($uid)
    {
        $count = Redis::get("bcc:{$uid}");
        if ($count === null) {
            $res = $this->eloquentClass::where('result_id', 'i' . $uid)->count();
            Redis::set("bcc:{$uid}", $res);
            $count = Redis::get("bcc:{$uid}");
        }
        return $count;
    }

    /**
     * 添加会员被收藏次数
     *
     * @param int $uid
     * @param string $type 1表示添加， 0表示减小
     * @return void
     */
    public function add_user_be_collect_count($uid, $type = '1')
    {
        $this->get_user_be_collect_count($uid);
        if ($type == '1') {
            Redis::incr("bcc:{$uid}");
        } else {
            Redis::decr("bcc:{$uid}");
        }
        return true;
    }

    /**
     * 获取会员收藏了多少
     *
     * @param int $uid
     * @return void
     */
    public function get_user_collect_count($uid)
    {
        $res = $this->eloquentClass::where('uid', $uid)->count();
//        $count = Redis::get("cc:{$uid}");
//        if ($count === null) {
//
//            Redis::set("cc:{$uid}", $res);
//            $count = Redis::get("cc:{$uid}");
//        }
        return $res;
    }

    /**
     * 更新会员收藏统计
     *
     * @param int $uid
     * @param string $type
     * @return void
     */
    public function add_user_collect_count($uid, $type = '1')
    {
        $this->get_user_collect_count($uid);
        if ($type == '1') {
            Redis::incr("cc:{$uid}");
        } else {
            Redis::decr("cc:{$uid}");
        }
        return true;
    }

    /**
     * 获取会员收藏记录
     *
     * @param int $uid
     * @param int $page
     * @param int $limit
     * @return void
     */
    public function use_uid_get_log($uid, $page, $limit)
    {
        $log = $this->eloquentClass::where('uid', $uid)->orderby('id', 'desc')->simplePaginate($limit);
        foreach ($log as $key => &$v) {
            if (substr($v->result_id, 0, 1) == 'i') {
                $udr = new \App\Api\Repositories\User\UserDetailRepositories();
                $detail = $udr->use_id_get_data(substr($v->result_id, 1, 100));
                if ($detail) {
                    $v->detail = $detail;
                } else {
                    unset($log->$key);
                }
            } else {
                $sr = new \App\Api\Repositories\Search\SearchExogenousDataRepositories();
                $v->detail = $sr->use_id_get_content(substr($v->result_id, 1, 100));
            }
        }
        return $log;
    }
}
